package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {

    //创建工厂
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    /**
     * 查询所有数据
     *
     * @return
     */
    @Override
    public List<Brand> selectAll() {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        List<Brand> brands = mapper.selectAll();
        //释放资源
        sqlSession.close();

        return brands;
    }

    /**
     * 添加数据
     *
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.add(brand);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Override
    public void deleteById(int id) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.deleteById(id);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    /**
     * 批量删除数据
     *
     * @param ids
     */
    @Override
    public void deleteByIds(int[] ids) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.deleteByIds(ids);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    /**
     * 修改数据
     *
     * @param brand
     */
    @Override
    public void update(Brand brand) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        mapper.update(brand);
        sqlSession.commit();

        //释放资源
        sqlSession.close();
    }

    /**
     * 分页查询
     *
     * @param currentPage 当前页码
     * @param pageSize    每页显示条数
     * @return
     */
    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;
        //查询每页显示数据
        List<Brand> rows = mapper.selectByPage(begin, size);
        //查询总条数
        int totalCount = mapper.selectTotalCount();
        //封装PageBean对象
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    /**
     * 根据ID回显数据
     *
     * @param id
     * @return
     */
    @Override
    public Brand selectById(int id) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //调用方法
        Brand brand = mapper.selectById(id);

        //释放资源
        sqlSession.close();
        return brand;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //连接数据库
        SqlSession sqlSession = factory.openSession();
        //使用Mapper代理
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;

        //处理brand条件，模糊表达式
        String brandName = brand.getBrandName();
        if (brandName != null && brandName.length() > 0) {
            brand.setBrandName("%" + brandName + "%");
        }
        String companyName = brand.getCompanyName();
        if (companyName != null && companyName.length() > 0) {
            brand.setCompanyName("%" + companyName + "%");
        }

        //查询每页显示数据
        List<Brand> rows = mapper.selectByPageAndCondition(begin, size,brand);
        //查询总条数
        int totalCount = mapper.selectTotalCountByCondition(brand);
        //封装PageBean对象
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

}
